package com.wceManage.manage.Emily;

import com.wceManage.beans.ECourse;
import com.wceManage.beans.EUser;
import com.wceManage.beans.JCours;
import com.wceManage.beans.SysUser;
import com.wceManage.beans.page.PageList;
import com.wceManage.manage.util.StringUtil;
import com.wceManage.manage.util.UserUtils;
import com.wceManage.services.CourseService;
import com.wceManage.services.ECourseService;
import com.wceManage.services.EmilyUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jiangwei
 * @since 18/6/4
 */
@Controller
@RequestMapping("Emily")
public class EmilyController {

    @Autowired
    private EmilyUserService userService;
    @Autowired(required = false)
    private CourseService courseService;
    @Autowired
    private ECourseService eCourseService;

    final static Logger log = LoggerFactory.getLogger(EmilyController.class);

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String login(HttpServletRequest request) {

        request.setAttribute("exp", "Emily");

        return "Emily/Index";
    }

    @RequestMapping(value = "page", method = RequestMethod.GET)
    public String page() {
        return "weCharErroe";

    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String add(HttpServletRequest request, EUser user) {

        userService.save(user);

        request.setAttribute("phone", user.getPhone());
        request.setAttribute("exp", user.getExp());


        JCours course = new JCours();
        course.setSystemId(courseService.findSystemId(user.getExp()));
        course.setLevel(2);
        List<JCours> courseBuy = courseService.findAllList(course);

        if (!CollectionUtils.isEmpty(courseBuy)) {
            request.setAttribute("course", courseBuy.get(0));
        } else {
            request.setAttribute("course", null);
        }

        return "Emily/activity";
    }


    @RequestMapping(value = "receive", method = RequestMethod.GET)
    public String receive(HttpServletRequest request, String phone, String exp) {

        request.setAttribute("phone", phone);
        request.setAttribute("exp", exp);

        int update = userService.updateStatusOfPhone(phone);

        List<JCours> courseBuy = new ArrayList<>();

        if (update > 0) {
            JCours course = new JCours();
            course.setSystemId(courseService.findSystemId(exp));
            course.setLevel(1);
            courseBuy = courseService.findAllList(course);
        }

        if (!CollectionUtils.isEmpty(courseBuy)) {
            request.setAttribute("course", courseBuy);
        } else {
            request.setAttribute("course", null);
        }

        JCours course2 = new JCours();
        course2.setSystemId(courseService.findSystemId(exp));
        course2.setLevel(2);
        courseBuy = courseService.findAllList(course2);

        if (!CollectionUtils.isEmpty(courseBuy)) {
            request.setAttribute("course2", courseBuy.get(0));
        } else {
            request.setAttribute("course2", null);
        }

        return "Emily/activity1";
    }

    @RequestMapping(value = "receiveBuy", method = RequestMethod.GET)
    public String receiveBuy(HttpServletRequest request, Integer courseId, String exp, String phone) {

        request.setAttribute("phone", phone);
        request.setAttribute("exp", exp);
        request.setAttribute("courseId", courseId);

        List<JCours> courseBuy = new ArrayList<>();

        JCours course = new JCours();
        course.setSystemId(courseService.findSystemId(exp));
        course.setLevel(1);
        courseBuy = courseService.findAllList(course);

        Map map = new HashMap<String, Object>();
        map.put("phone", phone);
        map.put("status", "1");

        List<EUser> eUser = userService.findAll(map);

        if (!CollectionUtils.isEmpty(eUser)) {
            request.setAttribute("course", courseBuy);
        } else {
            request.setAttribute("course", null);
        }

        course.setLevel(2);
        course.setId(courseId);
        courseBuy = courseService.findAllList(course);

        if (!CollectionUtils.isEmpty(courseBuy)) {
            JCours courseOne = courseBuy.get(0);

            if (courseOne != null) {
                ECourse eCourse = new ECourse();
                eCourse.setUrl(courseOne.getVideoUrl());
                if (StringUtil.isEmpty(courseOne.getLevel())) {
                    eCourse.setPrice(0.0);
                } else {
                    eCourse.setPrice(Double.valueOf(courseOne.getCourseNumber().toString()));
                }
                eCourse.setStatus("0");
                eCourse.setPhone(phone);
                eCourse.setType(courseOne.getLevel().toString());
                eCourse.setExp(exp);
                eCourse.setExp1(courseOne.getId() + "");

                eCourseService.save(eCourse);
            }

        }

        return "Emily/activity2";
    }


    @RequestMapping(value = "myCourse", method = RequestMethod.GET)
    public String queryCourse(HttpServletRequest request, String phone, String exp) {

        request.setAttribute("phone", phone);
        request.setAttribute("exp", exp);

        int update = userService.updateStatusOfPhone(phone);

        List<JCours> courseBuy = new ArrayList<>();

        if (update > 0) {
            JCours course = new JCours();
            course.setSystemId(courseService.findSystemId(exp));
            course.setLevel(1);
            courseBuy = courseService.findAllList(course);
        }

        Map map = new HashMap<String, Object>();
        map.put("phone", phone);
        map.put("status", "1");
        List<EUser> eUser = userService.findAll(map);

        if (!CollectionUtils.isEmpty(courseBuy) && !CollectionUtils.isEmpty(eUser)) {
            request.setAttribute("course", courseBuy);
        } else {
            request.setAttribute("course", null);
        }

        ECourse course2 = new ECourse();
        course2.setPhone(phone);
        course2.setStatus("1");
        course2.setExp(exp);
        courseBuy = eCourseService.findAll(course2);

        if (!CollectionUtils.isEmpty(courseBuy)) {
            request.setAttribute("course2", courseBuy.get(0));
        } else {
            request.setAttribute("course2", null);
        }

        return "Emily/activity3";
    }


    @RequestMapping(value = "viewCourse", method = RequestMethod.GET)
    public String viewCourse(HttpServletRequest request, Integer courseId, String exp, String phone) {

        request.setAttribute("phone", phone);
        request.setAttribute("exp", exp);
        request.setAttribute("courseId", courseId);

        HashMap<String, Object> map = new HashMap();
        map.put("courId", courseId);
        map.put("systemId", exp);

        request.setAttribute("course", courseService.queryById(map));

        return "Emily/course";
    }

    @RequestMapping(value = "queryUser", method = RequestMethod.POST)
    @ResponseBody
    public String queryUser(String phone) {

        String code = "400";

        Map map = new HashMap<String, Object>();
        map.put("phone", phone);

        List<EUser> eUser = userService.findAll(map);

        if (!CollectionUtils.isEmpty(eUser)) {
            code = "200";
        }
        return code;

    }

    /**
     * 查询列表
     *
     * @param course
     * @return
     */
    @RequiresPermissions("sys:ecourse:view")
    @RequestMapping(value = {"", "/", "list"})
    public String list(ECourse course, HttpServletRequest request)
            throws InstantiationException, IllegalAccessException {

        request.setAttribute("course", course);

        return "Emily/list";
    }

    /**
     * 异步获取list资源
     *
     * @param pageList
     * @param course
     * @return
     */
    @RequiresPermissions("sys:ecourse:view")
    @RequestMapping("asynList")
    @ResponseBody
    public PageList asynList(PageList pageList, ECourse course) {
        SysUser user = UserUtils.getUser();

        if (user != null) {
            course.setExp(courseService.findSystemId(user.getAccount()));
        }
        return eCourseService.queryPage(pageList, course);
    }

    @RequiresPermissions("sys:ecourse:delete")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id, HttpServletRequest request) {
        SysUser user = UserUtils.getUser();

        ECourse course = new ECourse();

        if (user != null) {
            course.setExp(courseService.findSystemId(user.getAccount()));
        }
        course.setId(id);

        Integer result = eCourseService.updateStatus(course);

        // TODO 成功失败结果页面
        return result;
    }

    @RequestMapping(value = "addQRCode", method = RequestMethod.GET)
    public String addQRCode(HttpServletRequest request) {

        return "Emily/QRCode";
    }

    @RequestMapping(value = "QRCodeList", method = RequestMethod.GET)
    public String QRCodeList(HttpServletRequest request) {

        List<Map>  QRCode = eCourseService.queryQRCode();

        request.setAttribute("QRCode", QRCode);

        return "Emily/QRCodeList";
    }

    @RequestMapping(value = "QRCode/save", method = RequestMethod.POST)
    public String QRCode(HttpServletRequest request, String imageUrl, String imageUrl1){
        SysUser user = UserUtils.getUser();

        HashMap<String, Object> map = new HashMap();
        map.put("systemId", courseService.findSystemId(user.getAccount()));
        map.put("imageUrl", imageUrl);
        map.put("imageUrl1", imageUrl1);

        eCourseService.QRCodeSave(map);

        List<Map>  QRCode = eCourseService.queryQRCode();

        request.setAttribute("QRCode", QRCode);

        return "Emily/QRCodeList";
    }

    @RequestMapping(value = "deleteCode", method = RequestMethod.GET)
    public String QRCode(HttpServletRequest request, String codeId){

        eCourseService.deleteCode(codeId);

        return "redirect:QRCodeList";

    }

}
